import { cmsUserLogin,cmsUserLoginOut } from '@/api/login';
import {
  setUserId,
  setToken,
  setName,
  setDescription,
  setRole,
  setPermission,
  setIsLogin,
  removeUserId,
  removeToken,
  removeName,
  removeDescription,
  removeRole,
  removePermission,
  removeIsLogin
} from "@/utils/UserCookies";

const user = {
  state: {
    userId: '',
    token: '',
    name: '',
    description: '',
    userRole: '',
    userPermission: [],
    isLogin: false
  },
  mutations: {
    // 设置登录用户ID
    SET_USER_ID: (state, userId) =>{
      state.userId = userId;
    },
    // 设置登录Token
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    // 设置用户昵称
    SET_NAME: (state, name) => {
      state.name = name;
    },
    // 设置用户描述
    SET_DESCRIPTION: (state, description) => {
      state.description = description;
    },
    // 设置用户角色
    SET_USER_ROLE: (state, userRole) => {
      state.userRole = userRole;
    },
    // 设置用户权限
    SET_USER_PERMISSION: (state, userPermission) => {
      state.userPermission = userPermission;
    },
    SET_IS_LOGIN: (state, isLogin) => {
      state.isLogin = isLogin;
    }
  },
  actions: {
    // 用户登录方法
    cmsUserLogin({ commit }, userInfo){
      // 去除空格
      const userAccount = userInfo.userAccount.trim();
      return new Promise((resolve,reject) => {
        // 异步进行接口调用
        let loginParams = { userAccount: userAccount, userPassword: userInfo.userPassword };
        cmsUserLogin(loginParams).then(response => {
          if (response.code === '200'){
            // 登录成功设置环境变量
            const data = response.data;
            commit('SET_USER_ID', data.userId);
            commit('SET_TOKEN', data.userToken);
            commit('SET_NAME', data.userName);
            commit('SET_DESCRIPTION', data.userDescription);
            commit('SET_USER_ROLE', data.userRole);
            commit('SET_USER_PERMISSION', data.userPermission);
            commit('SET_IS_LOGIN', true);
            // 设置缓存变量
            setUserId(data.userId);
            setToken(data.userToken);
            setName(data.userName);
            setDescription(data.userDescription);
            setRole(data.userRole);
            setPermission(data.userPermission);
            setIsLogin(true);
            resolve({code:200});
          } else {
            resolve({code:0, message: response.message});
          }
        })
      })
    },
    // 用户登出方法
    cmsUserLoginOut({ commit, userId }) {
      return new Promise((resolve, reject) => {
        let prams = {userId: userId};
        cmsUserLoginOut(prams).then(response => {
          if (response.code === '200'){
            // 清空环境变量
            commit('SET_USER_ID', '');
            commit('SET_TOKEN', '');
            commit('SET_NAME', '');
            commit('SET_DESCRIPTION', '');
            commit('SET_USER_ROLE', '');
            commit('SET_USER_PERMISSION', []);
            commit('SET_IS_LOGIN', false);
            // 清空缓存变量
            removeUserId();
            removeToken();
            removeName();
            removeDescription();
            removeRole();
            removePermission();
            removeIsLogin();
            resolve({code:200});
          } else {
            resolve({code:0, message: response.message});
          }
        })
      })
    },
  }
};

export default user;
